#!/usr/bin/ruby
require 'net/ssh'
require 'term/ansicolor'

class String
  include Term::ANSIColor
end

def colorize( data )
	c = Term::ANSIColor
	data.gsub!(/blocked using/, "#{c.red}blocked using caca#{c.clear}")
	data.gsub!(/disconnect/, "#{c.blue}disconnect caca#{c.clear}")
	data.gsub!(/LOGIN/, "#{c.yellow}LOGIN#{c.clear}")
	data.gsub!(/INFECTED (.*)/, 'VIRUS: \1'.red.bold)
	# colorize ips
	#data =~ /\b(?:\d{1,3}\.){3}\d{1,3}\b/
	data.gsub!(/(\b(?:\d{1,3}\.){3}\d{1,3}\b)/, '\1'.yellow.bold )
	return data
end

def do_tail( session, file )
	session.open_channel do |channel|
		channel.on_data do |ch, data|
			c = Term::ANSIColor
			puts c.clear
			data.each_line do |line|
			puts "[#{file}] -> #{colorize(line)}"
			end
		end
		channel.exec "tail -f #{file}"
	end
end

Net::SSH.start('rambo.soom.cl', 'root') do |session|
	do_tail session, "/var/log/messages"
	do_tail session, "/var/log/mail.log"
	do_tail session, "/var/log/mysql/mysql.err"
	session.loop
end
